<?php
include_once("controller/Bproduct.class.php");
include_once("controller/Bcategory.class.php");
include_once("controller/Bpage.class.php");
include_once("controller/Bnews.class.php");
include_once("controller/Paging.class.php");
require_once("model/category.class.php");
require_once("model/user.class.php");
require_once("model/page.class.php");
require_once("model/product.class.php");
require_once("model/news.class.php");
require_once("controller/upload.class.php");
require_once("includes/util.php");


//Mr phuc - Date:30-07-2008


/**
 * This function used as block function of smarty.
 * Purpose: check the category is in a list or not
 */
function is_in_list($params, $content, &$smarty, &$repeat) 
{ 
  if (isset($content)) 
  { 
  	$Id = $params['id']; 
    $selId = $params['select_id'];
  	
  	foreach($selId as $item)
  	{
  		if($Id == $item)
  			return $content;
  	}
  	  
  	return ""; 
  } 
} 

/**
 * manage class. Process for all function in manage tab of admin page
 */
class manage 
{
	var $db;
 	var $smarty;
 	
    function manage($db, $smarty)
	{
		//store pointer to db onject
		$this->db = $db;
		$this->smarty = $smarty;
	}
	
	
	/**
	 * Process all request related to category.
	 */
	function processCategory($sub1)
	{
		/*
		 * Save and build JSON object with newly added information
		 * and return to client
		 * 
		 */
		if(isset($_POST['act'])){
			/*
			 * 	The POST act is process for insert, update, delete on category
			 */	
			if($_POST['act'] == 'addnewcat'){
				Bcategory::addNewCategory();
			}else if($_POST['act'] == 'delcat'){
				Bcategory::deleteCategory();
			}else if($_POST['act'] == 'editcat'){
				Bcategory::editCategory();
			} elseif($_POST['act'] == 'change_position') {
				Bcategory::change_position();	
			} elseif($_POST['act'] == 'delphoto') {
				Bcategory::Delete_Photo();	
			}
		}else{
			$cat_type = category::getCatType($this->db);
			$this->smarty->assign("CATTYPE", $cat_type);
			$cat_manage= category::getCatManage($this->db);
			$this->smarty->assign("CATMANAGE", $cat_manage);
			//Load addition information in case edit category
			if($sub1 == "editcategory"){
				$catId = '';	
				if(isset($_GET['id']))
					$catId = $_GET['id'];
				//Load category information
				$cat = category::getCategoryById($this->db, $catId);
				$this->smarty->assign("THECAT", $cat);
				
				//In the case edit category we get all category 
				//except branch of editing category
				$path = "../".UPLOAD_DIR_LOGO;
				
				$photos = category::PhotosByCatId($this->db, $catId);
				$this->smarty->assign("PHOTOS",$photos);
				
				$this->smarty->assign("PATH", $path);
				$catList = category::getAllCategory($this->db, $catId);
				$this->smarty->assign("CATS", $catList);
			}else{
				if($_GET['act'] == 'newcategory'){
					$catList = category::getAllCategory($this->db, $catId);
					$this->smarty->assign("CATS", $catList);
				}else if($_GET['act'] == "position"){
					if(isset($_GET['subcat']) && $_GET['subcat']!="")
						$id = $_GET['catid'];
					else
						$id = 0;
					$cat = category::getCategoryById($this->db, $_GET['id']);		
					$listcat = category::get_category_by_level($this->db,$cat->cat_parent,$_GET['id']);
					$this->smarty->assign("THECAT", $cat);
					$this->smarty->assign("CATLEVEL", $listcat);
				}	
				//In the case add new category we get all category
				//and show in Parent field
				$countcat=category::countgetAllCat($this->db,$_SESSION["LC_ALL"]);
				$category['data']=category::getAllCatShareAdmin($this->db,$_SESSION['LC_ALL'],MAX_DEMO_PRODUCT_ADMIN,"list");
				$link = "?mod=".$_GET['mod']."&sub1=".$_GET['sub1'];
				$result = Paging::do_paging($countcat,$category, MAX_DEMO_PRODUCT_ADMIN, $link,"list");
				$result['PATH'] = "../".UPLOAD_DIR_LOGO;
				$this->smarty->assign("ALLCATS",$result);
			}
			
			$this->smarty->display('manage.tpl');
		}
	}
	
	/////////////////////////////
	
	
	/**
	 * Process all request related to Page functions.
	 * It'll call addNewPage, deletePage, updatePage
	 */
	function processPage($sub1)
	{
		//Register external smarty function.
		$this->smarty->register_block("is_in_list", "is_in_list"); 
		
		if(isset($_POST['act'])){
			/**
			 * 	The POST act is process for insert, update, delete on category
			 */	
		
			if($_POST['act'] == 'newpage'){
				Bpage::addNewPage();
			}else if($_POST['act'] == 'delpage'){
				Bpage::deletePage();
			}else if($_POST['act'] == 'editpage'){
				Bpage::updatePage();
			}
		}else{
			$act = "";
			if(isset($_GET["act"]))
				$act = $_GET["act"];
			
			if($act == "newpage" || $act == "editpage"){
				$users = user::getAllUser($this->db);	
				$this->smarty->assign("USERS", $users);
				
				$pages = page::getAllPage($this->db, "");
				$this->smarty->assign('PAGES', $pages); 
				
				//Build category checkbox list
				$catList = category::getAllCategory($this->db, null);
				
				$this->smarty->assign_by_ref('CATS', $catList);
				/*$arrCat = array();
				foreach($catList as $cat)
				{
					$arrCat[$cat->cat_ID] = 
						str_replace("—", "&nbsp;", $cat->temp_name);
				}
				
				$this->smarty->assign('cat_checkboxes', $arrCat); 
				*/
				/////////////////////////////////////////////////////
				
				if($act == "editpage")
				{
				
					$id = "";
					if(isset($_GET['id']))
					{
						//Load page information
						$id = $_GET['id'];
						if($id != "")
						{
							$page = page::getPageById($this->db, $id);
							
							$this->smarty->assign("THEPAGE", $page);
							
							$selcats = page::getCategoryOfPage($this->db, $id);
							$arrCat = array();
							if($selcats != null)
							{
								foreach($selcats as $cat)
								{
									$arrCat[count($arrCat)] = $cat->category_id;
								}
							}
							$this->smarty->assign('cat_selected_id', $arrCat);
						}
					}
				}
			}
			if(isset($_SESSION['page_message']))
			{
				$this->smarty->assign('page_message', $_SESSION['page_message']);
				unset($_SESSION['page_message']);
			}
			
			$this->showPage();
		}
		
		$this->smarty->unregister_block("is_in_list");
	}
	

	function processProduct($sub1){
		if(isset($_POST['act'])){
			$act = $_POST['act'];
			if($act == "addproduct"){
				Bproduct::addNewProduct();	
			}else if($act == "delproduct"){
				Bproduct::deleteProduct();	
			}else if($act == "editproduct"){
				Bproduct::editProduct();	
			}else if($act == "delphoto") {
				Bproduct::Delete_Photo();
			}	
		}	
		$act = "";
		if(isset($_GET["act"])){
			$act = $_GET["act"];
		}
		$this->smarty->assign("PATHLOGO","../".UPLOAD_DIR_LOGO);
		if($act == "newproduct" || $act=="editproduct"){
			$catList = category::getAllCategory($this->db, null);
			$this->smarty->assign("CATS", $catList);
			if($act == "editproduct"){
				if(isset($_GET['id'])){
					$id = $_GET['id'];
				}
				$photos = product::PhotosByProductId($this->db, $id);
				$this->smarty->assign("PATH","../".UPLOAD_PRODUCT_THUMB);
				$productone =  product::getProduct($this->db, $id);
				$productone->product_name = ($productone->product_name);
				$this->smarty->assign("PHOTOS",$photos);
				$this->smarty->assign("PRODUCT",$productone);
			}
			$this->showPage();
		}else{
			$countpro=product::countgetAllProducts($this->db,$_SESSION["LC_ALL"]);
 			$product['data']=product::getAllProductsSharePageAdmin($this->db,$_SESSION['LC_ALL'],MAX_DEMO_PRODUCT_ADMIN,"list");
 			$link = "?mod=".$_GET['mod']."&sub1=".$_GET['sub1'];
			$result = Paging::do_paging($countpro,$product, MAX_DEMO_PRODUCT_ADMIN, $link,"list");
			$result['PATH'] = "../".UPLOAD_PRODUCT_THUMB."/";
			$this->smarty->assign("ALLPRODUCT",$result);
			$this->showPage();
		}
	}
	
	function processNews($sub1)
	{
		if(isset($_POST['act'])){
			$act = $_POST['act'];
			if($act == "addnews"){
				Bnews::addNews();	
			}else if($act == "delnews"){
				Bnews::deleteNews();	
			}else if($act == "editnews"){
				Bnews::editNews();	
			}
		}	
		$act = "";
		if(isset($_GET["act"]))
			$act = $_GET["act"];
		if($act == "addnews" || $act=="editnews"){
			if($act == "editnews"){
				$id = "-1";
				if(isset($_GET['id']))
					$id = $_GET['id'];
				$this->smarty->assign("PATH","../".UPLOAD_NEWS_THUMB);
				$this->smarty->assign("NEWS", news::getNews($this->db, $id));
			}
			$this->showPage();
		}else{
			$countnews=news::countAllNews($this->db,$_SESSION["LC_ALL"]);
 			$news['data']=news::getAllNewsSharePageAdmin($this->db,$_SESSION["LC_ALL"],MAX_DEMO_NEWS_ADMIN,'list');
			$link = "?mod=".$_GET['mod']."&sub1=".$_GET['sub1'];
			$result = Paging::do_paging($countnews,$news, MAX_DEMO_NEWS_ADMIN, $link,"list");
			$result['PATH'] = "../".UPLOAD_NEWS_THUMB."/";
			$this->smarty->assign("ALLNEWS",$result);
			$this->showPage();
		}
	}

	
	/**
	 * This is common function used to parse request and
	 * set corresponding smarty variables.
	 * Smarty template will choose which page to show depend on
	 * request params and input variables.
	 */
	function showPage()
	{
		$act = "";
		if(isset($_GET["act"]))
		{
			$act = $_GET["act"];
		}
		
		$keyword = "";
		if(isset($_POST["s"]))
		{
			$keyword = $_POST["s"];
		}
		
		if($act == "" || $act == "search" || $act == "browse_cat")
		{
			if($act != "browse_cat")
			{
				$pages = page::getAllPage($this->db, $keyword);
			}
			else
			{
				$pages = page::getAllPageByCat($this->db, $_POST['cat'], null);
			}
			
			if($act == "browse_cat")
			{
				$this->smarty->assign("COMMENT", 
					_("Results for browse page by category"));
			}
			else if($act == "search")
			{
				$this->smarty->assign("COMMENT", 
					_("Results for search page"));
			}
			
			$this->smarty->assign("PAGES", $pages);
			
			//Get all category for search
			$catList = category::getAllCategory($this->db, null);
			$this->smarty->assign("CATS", $catList);
		}
		
		
		$this->smarty->display('manage.tpl');
	}
	
	/**
	 * This function will be call be index.php base on mod specified by GET param.
	 * Depend on sub1 param, it will call correspond function to process data
	 * for category function or page function.
	 */
	function process()
	{
		$sub1 = 'page';	
	
		if(isset($_GET['sub1']))
			$sub1 = $_GET['sub1'];
		
		if($sub1 == "")
			$sub1 = "page";
		
		if($sub1 == "category" || $sub1 == "editcategory")
			$this->processCategory($sub1);
		else if($sub1 == "page")
			$this->processPage($sub1);	
		else if($sub1 == "product")
			$this->processProduct($sub1);
		else if($sub1 == "news")
			$this->processNews($sub1);
	}
}
?>